<?php
/**
 * Created by PhpStorm.
 * User: moquan
 * Date: 2019/1/2
 * Time: 23:00
 */
require_once(dirname(__FILE__)."/../include/common.inc.php");

/*
 * 1、接收name
 * 2、处理成name 值成type
 * 3、取分类名称，(查询stepselect表中egroup 为type的，没有返回“错误”)
 * 4、取分类数据，(查询表中egroup 等于type 的，没有返回“错误”)
 * 5、取到对应组的数据，格式化成父子数据结构（带child的）
 * 6、加载模板
 *
 * */
// 处理name值
$flag = $_GET['flag'];
if(empty($_GET['flag'])){
    exit('错误');
}

// 得到type
$temp = explode('_',$flag);
$type = $temp[1];

/*----------分类名称-------------*/
$groupName = $dsql->GetOne("SELECT * FROM `#@__stepselect` where egroup = '{$type}'");
if(!is_array($groupName)){
    exit('无法获取分类信息');
}
/*----------分类数据-------------*/
$dsql->SetSql("SELECT * FROM `#@__sys_enum` WHERE egroup = '{$type}' ORDER BY disorder ASC, evalue ASC;");
$dsql->Execute();
$groupData = [];
while ($row = $dsql->GetArray()){
    $groupData[] = $row;
}
// 没有对应数据
if(empty($groupData)){
    exit('找不到数据');
}
//格式化
$enumData = format($groupData);
/*----加载模板文件---*/
if($type == 'aptitude'){
    include_once(DEDETEMPLATE.'/plus/my_test.htm');
} else {
    include_once(DEDETEMPLATE.'/plus/my_stepselect.htm');
}

/*-----------用到的函数/方法----------*/
function format($arr){
    //临时存放 [disorder => id]，二级根据 disorder 找 id 用到
    $tmp = [];
    foreach ($arr as $k => $v){
        if(0 === $v['disorder'] % 500){
            $tmp[$v['disorder']] = $v['id'];
        }
    }

    $data = [];
    foreach ($arr as $k => $v){
        $shang      = floor($v['disorder'] / 500);  //商
        $yu         = $v['disorder'] % 500; //余数
        $disorder   = $shang * 500 + $yu;   //计算得到的disorder

        if(0 === $yu){  //找一级
            $data[$v['id']] = !isset($data[$v['id']])? $v: array_merge($data[$v['id']], $v);
        }else{  //找二级
            $data[$tmp[$shang * 500]]['child'][$v['id']] = $v;
        }
    }
    return $data;
}

/*function getTop($em_aptitudes){

    $data = [];
    $html = "";
    foreach ($em_aptitudes as $key => $val){
        if($key % 500 == 0){
            $data[$key] = $val;
            $html.= "<span>{$val}</span>";
        }
    }
    return [
        'flag' => '公司资质',
        'origin' => $data,
        'html' => $html
    ];
}

function getByEvalue($em_aptitudes, $evalue){
    $data = [];
    foreach ($em_aptitudes as $key => $val){
        if($key > $evalue && $key < ($evalue+500)){
            $data[$key] = $val;
        }
    }
    return $data;
}*/

